/**
  ******************************************************************************
  * @file    nuc it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
**/
#include "gd32f10x.h"
#include "osObjects.h"  
#include "sys.h" 
#include "pulse.h" 
#include "daemonprocess.h" 
#include "gd25q_cmd.h"
#include "jiami.h"
#include "dataproc.h"

extern uint32_t ucTick;
uint32_t msg_buff[4];
timer_pulse_type led_pulse;
uint32_t led_task_cnt;

void jm_check_task(void * argument);

key_type key_obj;
switch_st_type sw1_obj;
out_st_type out1_obj;

volatile uint16_t key_edge_cnt[4];
volatile uint16_t key_found_edge_cnt[4];
volatile uint16_t key_lost_edge_cnt[4];

/*****************************************************************************//*!
*
*****************************************************************************/
void ledtask(void   *argument)
{
    uint8_t  key;
    
    gpio_init(GPIOF, GPIO_MODE_OUT_PP, GPIO_OSPEED_2MHZ, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
    
    gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, GPIO_PIN_0);
    gpio_init(GPIOC, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, GPIO_PIN_13);
    gpio_init(GPIOF, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ, GPIO_PIN_4 | GPIO_PIN_5);
    
    pulse_init(&led_pulse, ucTick);
    for(;;)
    {
        PulseCreat(&led_pulse, ucTick);
        osDelay(ms_ticks(10));
        led_task_cnt++;
        if(led_pulse.bPulse_100ms)
            PFout(0) = !PFout(0);
        if(led_pulse.bPulse_30ms)
            PFout(2) = !PFout(2);
        if(led_pulse.bPulse_1s)
            PFout(3) = !PFout(3);
        msg_buff[0]++;
        msg_buff[1]+=msg_buff[0];
        msg_buff[2]+=msg_buff[1];
        msg_buff[3]+=msg_buff[2];
        
        if(led_task_cnt > 200)
        {
             jm_check_task(argument);
        }
        
        if(led_pulse.bPulse_1s)
        {
            rt_mq_send(qid_mj,&msg_buff[0],Q_MJ_SIZE);
            rt_mq_send(qid_mj,&msg_buff[1],Q_MJ_SIZE);
        }
        
        key = 0;
        if(!gpio_input_bit_get(GPIOA, GPIO_PIN_0))
            key |= 0x01;
        if(!gpio_input_bit_get(GPIOC, GPIO_PIN_13))
            key |= 0x02;
        if(!gpio_input_bit_get(GPIOF, GPIO_PIN_5))
            key |= 0x04;
        if(!gpio_input_bit_get(GPIOF, GPIO_PIN_4))
            key |= 0x08;
        
        key_st_isr(&key_obj, key);
        key_obj.b_pulse_100ms = led_pulse.bPulse_100ms;
        key_st_handle(&key_obj);
        
        for(int cnt=0;cnt<4;cnt++)
        {
            if(key_obj.edge & (1UL << cnt))
                key_edge_cnt[cnt]++;
            if(key_obj.found_edge & (1UL << cnt))
                key_found_edge_cnt[cnt]++;
            if(key_obj.lost_edge & (1UL << cnt))
                key_lost_edge_cnt[cnt]++;
        }
        
        sw1_obj.b_sample = led_pulse.bPulse_20ms;
        sw1_obj.b_time_pulse = led_pulse.bPulse_100ms;
        switch_handle(&sw1_obj, key & 0x01);
        
        out1_obj.b_time_pulse = led_pulse.bPulse_20ms;
        out_st_handle(&out1_obj, key & 0x01);
    }
}
